<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * 后台报名信息管理控制器
 * @author huajie <banhuajie@163.com>
 */
class GoodsController extends AdminController {



    /**
     * 显示左边菜单，进行权限控制
     * @author huajie <banhuajie@163.com>
     */
    protected function getMenu(){
        //获取动态分类
        $cate_auth  =   AuthGroupModel::getAuthCategories(UID);	//获取当前用户所有的内容权限节点
        $cate_auth  =   $cate_auth == null ? array() : $cate_auth;
        $cate       =   M('Category')->where(array('status'=>1))->field('id,title,pid,allow_publish')->order('pid,sort')->select();

        //没有权限的分类则不显示
        if(!IS_ROOT){
            foreach ($cate as $key=>$value){
                if(!in_array($value['id'], $cate_auth)){
                    unset($cate[$key]);
                }
            }
        }

        $cate           =   list_to_tree($cate);	//生成分类树

        //获取分类id
        $cate_id        =   I('param.cate_id');
        $this->cate_id  =   $cate_id;

        //是否展开分类
        $hide_cate = false;
        if(ACTION_NAME != 'recycle' && ACTION_NAME != 'draftbox' && ACTION_NAME != 'mydocument'){
            $hide_cate  =   true;
        }

        //生成每个分类的url
        foreach ($cate as $key=>&$value){
            $value['url']   =   'Article/index?cate_id='.$value['id'];
            if($cate_id == $value['id'] && $hide_cate){
                $value['current'] = true;
            }else{
                $value['current'] = false;
            }
            if(!empty($value['_child'])){
                $is_child = false;
                foreach ($value['_child'] as $ka=>&$va){
                    $va['url']      =   'Article/index?cate_id='.$va['id'];
                    if(!empty($va['_child'])){
                        foreach ($va['_child'] as $k=>&$v){
                            $v['url']   =   'Article/index?cate_id='.$v['id'];
                            $v['pid']   =   $va['id'];
                            $is_child = $v['id'] == $cate_id ? true : false;
                        }
                    }
                    //展开子分类的父分类
                    if($va['id'] == $cate_id || $is_child){
                        $is_child = false;
                        if($hide_cate){
                            $value['current']   =   true;
                            $va['current']      =   true;
                        }else{
                            $value['current'] 	= 	false;
                            $va['current']      =   false;
                        }
                    }else{
                        $va['current']      =   false;
                    }
                }
            }
        }

        $this->assign('nodes',      $cate);
        $this->assign('cate_id',    $this->cate_id);

        //获取面包屑信息
        $nav = get_parent_category($cate_id);
        $this->assign('rightNav',   $nav);

        //获取回收站权限
        $show_recycle = $this->checkRule('Admin/article/recycle');
        $this->assign('show_recycle', IS_ROOT || $show_recycle);
        //获取草稿箱权限
        $this->assign('show_draftbox', C('OPEN_DRAFTBOX'));
    }


    /**
     * 商品管理
     */
    public function index(){
        $list   = $this->lists(M('goods'), array(),'id asc');
        $cateList = M("cate")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('_list', $list);
        $this->meta_title = '商品信息';
        $this->display();
    }

    public function addGoods(){
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);

        $brandList = M("brand")->where(array('cate_id'=>$cateList[0]['id']))->select();
        $this->assign('brandList',$brandList);

        $modelList = M("model")->where(array('brand_id'=>$brandList[0]['id']))->select();
        $this->assign('modelList',$modelList);

        $this->meta_title = '新增商品';
        $this->display('editgoods');
    }

    public function editGoods(){
        $id=I('get.id');
        $data=M('goods')->where(array('id'=>$id))->find();
        $data['good_img_arr']=explode(',',$data['good_img']);
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);

        $brandList = M("brand")->where(array('cate_id'=>$cateList[0]['id']))->select();
        $this->assign('brandList',$brandList);

        $modelList = M("model")->where(array('brand_id'=>$data['id']))->select();
        $this->assign('modelList',$modelList);

        $this->assign('info',$data);
        $this->meta_title = '编辑商品';
        $this->display('editgoods');
    }

   //获取商品品牌
    public function ajaxGetGoodBrand(){
      $cate_id = I('get.cate_id') ? I('get.cate_id') : 0;  
      $brandlList = M("brand")->where(array('cate_id'=>$cate_id))->select();
      $this->assign('brandlList',$brandlList);
      $html=$this->fetch('ajaxgetgoodbrand');
      $this->ajaxReturn($html);
    }

   //获取商品型号
    public function ajaxGetGoodModel(){
      $brand_id = I('get.brand_id') ? I('get.brand_id') : 0;  
      $modelList = M("model")->where(array('brand_id'=>$brand_id))->select();
      $this->assign('modelList',$modelList);
      $html=$this->fetch('ajaxgetgoodmodel');
      $this->ajaxReturn($html);
    }

    public function saveGoods(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        $data['good_img']=implode(',',$data['good_img']);

        if(empty($data['id'])){ //新增数据
            $res = M('goods')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('index') );
            }
        } else { //更新数据
            $status = M('goods')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('index') );
            }
        }
    }

    public function deleteGoods(){
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
        }else{
            $where['id']=$ids;
        }

        if( M('goods')->where($where)->delete() !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }
    }




    //商品规格
    public function specList(){
        $where=array();
        I('cate_id') && $where['cate_id']=I('cate_id') ;
        $specList   = $this->lists(M('spec'), $where,'id asc');
        foreach($specList as $k => $v)
        {       // 获取规格项
            $arr = D('Spec')->getSpecItem($v['id']);
            $specList[$k]['spec_item'] = implode(' , ', $arr);
        }

        $cateList = M("cate")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('_list', $specList);

        $this->meta_title = '商品规格信息';
        $this->display();
    }


    //新增商品分类
    public function addSpec(){
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);
        $this->meta_title = '新增商品规格';
        $this->display('editspec');
    }


    public function editSpec(){
        $id=I('get.id');
        $data=M('spec')->where(array('id'=>$id))->field('id,cate_id,name,order')->find();
        $items =  D('Spec')->getSpecItem($id);
        $data['items'] = implode(PHP_EOL, $items);
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);
        $this->assign('info',$data);
        $this->meta_title = '编辑商品规格';
        $this->display('editspec');
    }


    public function saveSpec(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        unset($data['items']);
        if(empty($data['id'])){ //新增数据
            $res = M('spec')->add($data);
            D('Spec')->afterSave($res);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('specList') );
            }
        } else { //更新数据
            $status = M('spec')->save($data);
            D('Spec')->afterSave($data['id']);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('specList') );
            }
        }
    }


    /**删除规格
     * @param array $where
     */
    public function deleteSpec() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
            $map['spec_id']=array('in',$ids);
        }else{
            $where['id']=$ids;
            $map['spec_id']=$ids;
        }

        if( M('spec')->where($where)->delete() !== false &&  M('SpecItem')->where($map)->delete() !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }




    //商品品牌
    public function brandList(){
        $where=array();
        I('cate_id') && $where['cate_id']=I('cate_id') ;
        $List   = $this->lists(M('brand'), $where,'sort desc');
        $cateList = M("cate")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('_list', $List);

        $this->meta_title = '品牌信息';
        $this->display();
    }


    //新增品牌
    public function addBrand(){
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);
        $this->meta_title = '新增品牌';
        $this->display('editbrand');
    }


    public function editBrand(){
        $id=I('get.id');
        $data=M('brand')->where(array('id'=>$id))->field('id,cate_id,name,sort')->find();
        $cateList = M("cate")->select();
        $this->assign('cateList',$cateList);
        $this->assign('info',$data);
        $this->meta_title = '编辑品牌';
        $this->display('editbrand');
    }


    public function saveBrand(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('brand')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('brandList') );
            }
        } else { //更新数据
            $status = M('brand')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('brandList') );
            }
        }
    }

    /**删除规格
     * @param array $where
     */
    public function deleteBrand() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
            $map['brand_id']=array('in',$ids);
        }else{
            $where['id']=$ids;
            $map['brand_id']=$ids;
        }

        if( M('brand')->where($where)->delete() !== false &&  M('model')->where($map)->delete() !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }




    //商品型号
    public function modelList(){
        $where=array();
        I('brand_id') && $where['brand_id']=I('brand_id') ;
        $List   = $this->lists(M('model'), $where,'sort desc');
        $brandList = M("brand")->select();
        $brandList=get_id_key($brandList,'id');
        $this->assign('brandList',$brandList);
        $this->assign('_list', $List);

        $this->meta_title = '型号列表';
        $this->display();
    }


    //新增型号
    public function addModel(){
        $brandList = M("brand")->select();
        $this->assign('brandList',$brandList);
        $this->meta_title = '新增型号';
        $this->display('editmodel');
    }


    public function editModel(){
        $id=I('get.id');
        $data=M('model')->where(array('id'=>$id))->field('id,brand_id,name,sort')->find();
        $brandList = M("brand")->select();
        $this->assign('brandList',$brandList);
        $this->assign('info',$data);
        $this->meta_title = '编辑品牌';
        $this->display('editmodel');
    }


    public function saveModel(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('model')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('modelList') );
            }
        } else { //更新数据
            $status = M('model')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('modelList') );
            }
        }
    }

  




    //商品分类
    public function cateList(){
        $list   = $this->lists(M('cate'), array(),'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '商品分类信息';
        $this->display();
    }



    //新增商品分类
    public function addCate(){
        $this->meta_title = '新增商品分类';
        $this->display('editcate');
    }

    public function editCate(){
        $id=I('get.id');
        $data=M('cate')->where(array('id'=>$id))->field('id,name')->find();
        $this->assign('info',$data);
        $this->meta_title = '编辑商品分类';
        $this->display('editcate');
    }


    public function saveCate(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('cate')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('cateList') );
            }
        } else { //更新数据
            $status = M('cate')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('cateList') );
            }
        }
    }


    /**删除分类
     * @param array $where
     */
    public function deleteCate() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $cate['id']=array('in',$ids);
            $spec['cate_id']=array('in',$ids);
            $brand['cate_id']=array('in',$ids);
        }else{
            $cate['id']=$ids;
            $spec['cate_id']=$ids;
            $brand['cate_id']=$ids;
        }

        $re1= M('cate')->where($cate)->delete();
        $re2= M('spec')->where($spec)->delete();
        $re3= M('brand')->where($brand)->delete();

        if( $re1 !== false &&  $re2 !== false && $re3!=false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }




    /**
     *导出excel
     */
   public function exportExcel(){
       $data=I('get.');
       if(!isset($data['type']) || empty($data['type'])){
           $data['type']='all';
       }
       $map['a.type']=2;
       $list=array();
       switch ($data['type']){
           case 'all':
               $status=I('get.status');
               if($status=='' || $status==null){
                   $map['a.status']=array('gt',-1);
               }else{
                   $map['a.status']=$status;
               }
               $id=I('get.id');
               if($id!='' && $id!=null){
                   $map['a.introducer_id']=$id;
               }
               $major=I('get.major');
               if($major!='' && $major!=null){
                   $where['title']=array('like','%'.$major.'%');
                   $major=M('major')->where($where)->select();
                   $ids = array_column($major, 'id');
                   $map['a.major']=array('in',$ids);
               }
               $name=I('get.name');
               if($name!='' && $name!=null){
                   $map['a.name']=array('like','%'.$name.'%');
               }

               $ID_number=I('get.ID_number');
               if($ID_number!='' && $ID_number!=null){
                   $map['a.ID_number']=array('like','%'.$ID_number.'%');
               }
               $introducer=I('get.introducer');
               if($introducer!='' && $introducer!=null){
                   $map['a.introducer']=array('like','%'.$introducer.'%');
               }

               $list=M('student_info')
                   ->alias('a')
                   ->join('vo_major as b ON a.major=b.id')
                   ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                   ->where($map)
                   ->select();
               break;
           case 'agent':
               $map['introducer_id']=$data['id'];
               $list=M('student_info')
                   ->alias('a')
                   ->join('vo_major as b ON a.major=b.id')
                   ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                   ->where($map)
                   ->select();
               break;
           case 'department':
               $depart=M('department_info')->where(array('id'=>$data['id']))->field('id,title')->find();
               $agent=M('agent')->where(array('department'=>$depart['id']))->field('uid,username')->select();
               if(empty($agent)){
                   $ids = array_column($agent, 'uid');
                   $map['introducer_id']=array('in',$ids);
                   $list=M('student_info')
                       ->alias('a')
                       ->join('vo_major as b ON a.major=b.id')
                       ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                       ->where($map)
                       ->select();
               }
               break;
           case 'major':
               $map['major']=$data['id'];
               $list=M('student_info')
                   ->alias('a')
                   ->join('vo_major as b ON a.major=b.id')
                   ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                   ->where($map)
                   ->select();
               break;
       }

       if(empty($list)){
           $this->error("没有数据可以导出");
       }
       foreach ($list as $k=>&$vo){
           $vo['create_time']=time_format($vo['create_time']);
           $vo['update_time']=time_format($vo['update_time']);
           $vo['manage_update_time']=time_format($vo['manage_update_time']);
           $vo['pay_evidence']=empty($vo['pay_evidence'])?"未缴费":'已缴费';
           if($vo['status']==0){
               $vo['status']='未审核';
           }else if($vo['status']==1){
               $vo['status']='已审核';
           }
       }
       exportexcel($list,array('编号','姓名','身份证号','家庭住址','毕业院校','专业','介绍人','介绍人电话','是否缴费','首次提交时间','最后一次修改时间','管理员修改时间','审核状态'),'非自主报名信息');
   }





    /**
     * 设置一条或者多条数据的状态
     * @author huajie <banhuajie@163.com>
     */
    public function setStatus($model='student_info'){
        return parent::setStatus('student_info');
    }



}
